package com.coolgor.coldot.dao.auth;

import java.util.List;

import com.coolgor.coldot.entity.auth.Function;
import org.apache.ibatis.annotations.*;


@Mapper
public interface FunctionDao {

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_function
			WHERE
				function_id = #{functionID}					
		</script>
	""")
	@Results(id = "FunctionMap", value = {
		@Result(property = "functionID", column = "function_id"),
		@Result(property = "functionName", column = "function_name"),
		@Result(property = "functionURL", column = "function_url"),
		@Result(property = "parentID", column = "parent_id"),
		@Result(property = "priority", column = "priority"),
		@Result(property = "status", column = "status"),
		@Result(property = "title", column = "title"),
		@Result(property = "remark", column = "remark")
	})
	Function queryFunctionById(Long functionID);



	@Select("""
		<script>
			SELECT
				function_id,function_name,function_url,parent_id,priority,title,status,remark
			FROM
				tb_coldot_auth_function
			<where>
				<if test="status != null">
					and status = #{status}
				</if>
				and function_id = #{parentID}
			</where>
			UNION
			SELECT
				function_id,function_name,function_url,parent_id,priority,title,status,remark
			FROM
			(
				SELECT
					t1.*,
					if(find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', function_id), 0) as ischild
				FROM
				(
					SELECT
						*
					FROM
						tb_coldot_auth_function t
					<where>
						<if test="status != null">
							and status = #{status}
						</if>
					</where>
					ORDER BY
						parent_id, function_id
				) t1,
				(SELECT @pids := #{parentID}) t2
			) t3
			WHERE ischild != 0			
		</script>
	""")
	@ResultMap("FunctionMap")
	List<Function> queryFunctionTreeByParentID(@Param("parentID") Long parentID,
												@Param("status") String status);




	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_function
			<where>		
				parent_id
				IN
				<foreach collection="parentIDs" item="item" index="index" open="(" separator="," close=")">
					${item}
				</foreach>
				<if test="status != null">
					and status = #{status}
				</if>
			</where>
			ORDER BY
				parent_id,priority		
		</script>
	""")
	@ResultMap("FunctionMap")
	List<Function> queryFunctionListByParentIDs(@Param("parentIDs") List<Long> parentIDs,
												@Param("status") String status);




	@Select("""
		<script>
			SELECT
				t2.*
			FROM
				tb_coldot_auth_role_function t1
				LEFT JOIN
				tb_coldot_auth_function t2
				ON t1.function_id = t2.function_id
			<where>		
				parent_id
				IN
				<foreach collection="parentIDs" item="item" index="index" open="(" separator="," close=")">
					${item}
				</foreach>
				<if test="roleID != null">
					and t1.role_id = #{roleID}
				</if>
				<if test="status != null">
					and status = #{status}
				</if>
			</where>
			ORDER BY
				parent_id,priority
		</script>
	""")
	@ResultMap("FunctionMap")
	List<Function> queryFunctionListByParentIDsInRoleID(@Param("parentIDs") List<Long> parentIDs,
														@Param("roleID") Long roleID,
														@Param("status") String status);




	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_function
			<where>
				<if test="functionCondition.functionID != 0">
					and function_id = #{functionCondition.functionID}
				</if>
				<if test="functionCondition.functionName != null">
					and function_name like '%${functionCondition.functionName}%'
				</if>
				<if test="functionCondition.functionURL != null">
					and function_url like '%${functionCondition.functionURL}%'
				</if>
				<if test="functionCondition.parentID != 0">
					and parent_id = #{functionCondition.parentID}
				</if>
				<if test="functionCondition.priority != null">
					and priority = #{functionCondition.priority}
				</if>
				<if test="functionCondition.status != 0">
					and status = #{functionCondition.status}
				</if>
				<if test="functionCondition.title != null">
					and title = #{functionCondition.title}
				</if>
				<if test="functionCondition.remark != null">
					and remark like '%${functionCondition.remark}%'
				</if>
			</where>
			LIMIT #{rowIndex},#{pageSize};	
		</script>
	""")
	@ResultMap("FunctionMap")
	List<Function> queryFunctionList(@Param("functionCondition") Function functionCondition,
										@Param("rowIndex") int rowIndex,
										@Param("pageSize") int pageSize);


	@Select("""
		<script>
			SELECT
				count(1)
			FROM
				tb_coldot_auth_function
			<where>
				<if test="functionCondition.functionID != 0">
					and function_id = #{functionCondition.functionID}
				</if>
				<if test="functionCondition.functionName != null">
					and function_name like '%${functionCondition.functionName}%'
				</if>
				<if test="functionCondition.functionURL != null">
					and function_url like '%${functionCondition.functionURL}%'
				</if>
				<if test="functionCondition.parentID != null">
					and parent_id = #{functionCondition.parentID}
				</if>
				<if test="functionCondition.priority != null">
					and priority = #{functionCondition.priority}
				</if>
				<if test="functionCondition.status != 0">
					and status = #{functionCondition.status}
				</if>
				<if test="functionCondition.title != null">
					and title = #{functionCondition.title}
				</if>
				<if test="functionCondition.remark != null">
					and remark like '%${functionCondition.remark}%'
				</if>
			</where>
		</script>
	""")
	int queryFunctionCount(@Param("functionCondition") Function functionCondition);



	@Insert("""
		<script>
			INSERT INTO tb_coldot_auth_function
				(function_name,function_url,parent_id,priority,title,status,remark)
			VALUES
				(#{functionName},#{functionURL},#{parentID},#{priority},#{title},#{status},#{remark})
		</script>
	""")
	@Options(useGeneratedKeys = true, keyColumn = "function_id", keyProperty = "functionID")
	int insert(Function function);


	@Update("""
		<script>
			UPDATE tb_coldot_auth_function
			<set>
				<if test="functionName != null">function_name=#{functionName},</if>
				<if test="functionURL != null">function_url=#{functionURL},</if>
				<if test="parentID != null">parent_id=#{parentID},</if>
				<if test="priority != null">priority=#{priority},</if>
				<if test="title != null">title=#{title},</if>
				<if test="status != null">status=#{status},</if>
				<if test="remark != null">remark=#{remark}</if>
			</set>
			WHERE\s
				function_id = #{functionID}
		</script>	
	""")
	int update(Function function);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_auth_function
			WHERE
				function_id = #{functionID}
		</script>	
	""")
	int delete(Function function);

	
	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_auth_function
			WHERE
				function_id
				IN
				<foreach collection="list" item="functionID" open="(" separator="," close=")">
					#{functionID}
				</foreach>		 
		</script>	
	""")
	int deleteBatch(List<Long> functionIDList);
	
}